/*
 * UploadHubList.java
 *
 * Created on 16 de Dezembro de 2004, 19:26
 */

package ceirinhashls.gui;
import ceirinhashls.SaveHublist;
import ceirinhashls.UploadHubList;
import ceirinhashls.util.Settings;
import javax.swing.*;

/**
 *
 * @author  botelhodaniel
 */
public class UploadHubListFrame extends javax.swing.JFrame {
    private MainFrame mf;
    /** Creates new form UploadHubList */
    public UploadHubListFrame(MainFrame mf) {
        this.mf = mf;
        
        initComponents();
        doDefault();
        update();
    }
    private void doDefault(){
        jRadioButton1.setSelected(Settings.WEB_SERVER);
        jRadioButton2.setSelected(Settings.LOCAL_FILE);
        jTextField3.setText(Settings.LOCAL_FILE_PATH);
        jTextField4.setText(Settings.REMOTE_FILE_PATH);
        jTextField1.setText(Settings.FTP_SERVER_ADDRESS);
        jTextField2.setText(Settings.FTP_SERVER_USER);
        jPasswordField1.setText(Settings.FTP_SERVER_PASSWORD);
    }
    private void setDefault(){
        //Settings.WEB_SERVER = jRadioButton1.isSelected();
        //Settings.LOCAL_FILE = jRadioButton2.isSelected();
        Settings.LOCAL_FILE_PATH = jTextField3.getText().trim();
        Settings.REMOTE_FILE_PATH = jTextField4.getText().trim();
        Settings.FTP_SERVER_ADDRESS = jTextField1.getText().trim();
        Settings.FTP_SERVER_USER = jTextField2.getText().trim();
        Settings.FTP_SERVER_PASSWORD = new String(jPasswordField1.getPassword()).trim();
    }
    
    private void update(){
        jTextField1.setEditable(jRadioButton1.isSelected());
        jTextField1.setOpaque(jRadioButton1.isSelected());
        jTextField2.setEditable(jRadioButton1.isSelected());
        jTextField2.setOpaque(jRadioButton1.isSelected());
        jTextField3.setEditable(jRadioButton2.isSelected());
        jTextField3.setOpaque(jRadioButton2.isSelected());
        jTextField4.setEditable(jRadioButton1.isSelected());
        jTextField4.setOpaque(jRadioButton1.isSelected());
        jPasswordField1.setEditable(jRadioButton1.isSelected());
        jPasswordField1.setOpaque(jRadioButton1.isSelected());
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jPanel6 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jPanel9 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jPanel3.setLayout(new java.awt.BorderLayout());

        jButton1.setText(StringDef.OK);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel5.add(jButton1);

        jButton2.setText(StringDef.CANCEL);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel5.add(jButton2);

        jPanel3.add(jPanel5, java.awt.BorderLayout.SOUTH);

        jPanel7.setLayout(new java.awt.BorderLayout());

        jPanel4.setLayout(new java.awt.GridLayout(4, 0, 2, 2));

        jPanel4.setBorder(new javax.swing.border.TitledBorder(""));
        jLabel4.setText(StringDef.LOCAL_FILE);
        jPanel4.add(jLabel4);

        jTextField3.setColumns(20);
        jPanel4.add(jTextField3);

        jLabel5.setText(StringDef.REMOTE_FILE);
        jPanel4.add(jLabel5);

        jTextField4.setColumns(20);
        jPanel4.add(jTextField4);

        jPanel7.add(jPanel4, java.awt.BorderLayout.CENTER);

        jPanel8.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));

        jCheckBox1.setSelected(true);
        jCheckBox1.setText(StringDef.START_TRANSFER_NOW);
        jPanel8.add(jCheckBox1);

        jPanel7.add(jPanel8, java.awt.BorderLayout.SOUTH);

        jPanel3.add(jPanel7, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel3);

        jPanel1.setLayout(new java.awt.GridLayout(6, 1));

        jPanel1.setBorder(new javax.swing.border.TitledBorder(StringDef.SITE_DETAILS));
        jLabel1.setText(StringDef.HOST);
        jPanel1.add(jLabel1);

        jTextField1.setColumns(20);
        jPanel1.add(jTextField1);

        jLabel2.setText(StringDef.USER);
        jPanel1.add(jLabel2);

        jPanel1.add(jTextField2);

        jLabel3.setText(StringDef.PASSWORD);
        jPanel1.add(jLabel3);

        jPanel1.add(jPasswordField1);

        jPanel6.add(jPanel1);

        jPanel2.add(jPanel6);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        jRadioButton1.setSelected(true);
        jRadioButton1.setText(StringDef.WEB_FTP_SERVER);
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jPanel9.add(jRadioButton1);

        jRadioButton2.setText(StringDef.LOCAL_MACHINE);
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jPanel9.add(jRadioButton2);

        getContentPane().add(jPanel9, java.awt.BorderLayout.NORTH);

        pack();
    }//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        doDefault();
        setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        setDefault();
        if(jCheckBox1.isSelected())
            try{
                if(jRadioButton2.isSelected())
                    new SaveHublist(mf.hublistToIterator(),jTextField3.getText().trim());
                else{
                    java.io.File f =java.io.File.createTempFile("temp",(Settings.REMOTE_FILE_PATH.toLowerCase().endsWith(".xml"))? ".xml":".config");
                    new SaveHublist(mf.hublistToIterator(),f);
                    new UploadHubList(
                    Settings.FTP_SERVER_ADDRESS,
                    Settings.FTP_SERVER_USER,
                    Settings.FTP_SERVER_PASSWORD,
                    Settings.REMOTE_FILE_PATH,
                    f.getAbsolutePath()
                    );
                }
            }catch(Exception e){e.printStackTrace();}
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
        jRadioButton1.setSelected(false);
        jRadioButton2.setSelected(true);
        update();
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:
        jRadioButton1.setSelected(true);
        jRadioButton2.setSelected(false);
        update();
    }//GEN-LAST:event_jRadioButton1ActionPerformed
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        setVisible(false);
    }//GEN-LAST:event_exitForm
 
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration//GEN-END:variables
    
}
